package com.hdaccp.foodsales.shiro;

import com.hdaccp.foodsales.dao.GroupsDao;
import com.hdaccp.foodsales.dao.UserDao;
import com.hdaccp.foodsales.entity.Groups;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sound.sampled.AudioFileFormat;

public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserDao userDao;
    @Autowired
    private GroupsDao groupsDao;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo() ;
        String account =(String)principalCollection.getPrimaryPrincipal();

        Groups groups = groupsDao.getRoleByAccount(account);
        System.out.println(groups+"--------");

        info.addRole(groups.getRcode());
        System.out.println(info+"-----1");
        return info;

    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String username =token.getUsername();
        String pwd =userDao.getPwd(username);
        if (pwd==null || "".equals(pwd)){
            return null;

        }
        AuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd,"myrealm") ;
        //认证
        return info;
    }
}
